<?php
session_start();
error_reporting(0);
ini_set("memory_limit","100M");
require_once("includes/geoiploc.php");

error_reporting(0);
if (isset($_REQUEST['_SESSION']))
die('Unauthorized Access has been denied!');

require_once("../includes/dbconfig.php");
require_once("includes/functions.php");
require_once("includes/settings.php");


if(!isset($_GET['gid']) || preg_match('/[^0-9]/', $_GET['gid']))
{
    die("Invalid Gateway");
}



$ip = getIP();
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$c = geoip_country_code_by_addr($gi, getIP());



$gwid = safeGet($_GET['gid']);
$aff_id = safeGet($_GET['aff_id']);

//check if ip is banned to access this site
$isp = mysql_query("SELECT * FROM ipbans WHERE ip = '".getIP()."'");
if(mysql_num_rows($isp))
{
  die( "Your ip is banned to use this website!" );
  return;
}


$sql1 = mysql_query("SELECT * FROM gateways WHERE gid = '".makesafe($gwid)."' AND uid = '".makesafe($aff_id)."' LIMIT 1");

if(mysql_num_rows($sql1))
{

    $r = mysql_fetch_object($sql1);
	
	$width = $r->width;
	$height = $r->height;
	$opacity = $r->overlay_opacity;
	$overlay_color = $r->overlay_color;
	$load_time = $r->start_delay;
	$bg_color = $r->bg_color;
	$border_color = $r->border_color;
	$border_size = $r->border_size;
	$mobile = 0;
	$include_close = $r->include_close;
	
	
    if($load_time != -1){
    if(preg_match('/[^0-9]/', $load_time) || empty($load_time))
	{
	    $load_time = 0;	
	}
	}


    //Countries Targetting - Show gateways only in availavble countries
	
	$countries = $r->countries;
	
	if($countries != "All" && !empty($countries))
	{

		$tcountries = explode(",",$countries);
		if(!in_array($c,$tcountries)){
		die("Sorry, Gateway not available for this country.");	
		return false;  //gateway not available in this country.     
		}
	}
	//End Countries targetting.


   if(empty($width))
   $width = '630';
   
   if(empty($height))
   $height = '420';




  if((isset($_SESSION[SITE_NAME.'__hst_gwADN_cl_sessID__XCLD']) && sessionExists($_SESSION[SITE_NAME.'__hst_gwADN_cl_sessID__XCLD'])) || validateSession($_COOKIE[SITE_NAME.'__hst_gwADN_cl_sessID__XCLD'.$gwid], $gwid, $aff_id)){ // if session is registered and validated, no need to show widget. unlock content!


  if(validateSession($_SESSION[SITE_NAME.'__hst_gwADN_cl_sessID__XCLD'], $gwid, $aff_id) || validateSession($_COOKIE[SITE_NAME.'__hst_gwADN_cl_sessID__XCLD'.$gwid], $gwid, $aff_id)){
  return;  //UNLOCK!!! 
  
  }

	  
	  
  }else
  
  //As session is not registered, or not yet validated, register new session.
  $sessId = registerSessId($gwid, $aff_id);	

  while(empty($sessId))
  {
     $sessId = registerSessId($gwid, $aff_id);		  
  }


  if(!$sessId)
  {
	  die("Some error occured, please try later, or contact admin to resolve this issue.");
  }
  

  
  $_SESSION['__du_gw_cl_sessID__XCLD'] = $sessId;	
  
  

	
	
   $randomHash = $sessId;
	
	

	
	
	
}else
{ 
    echo "Invalid Gateway.";
 	return;
}


//we have to set frame size on this page, thats why we have to get heigh, width, opacity etc for the gateway from db.

?>
var ssid = '<?=$sessId?>';
var SITE_URL = '<?=SITE_URL?>';    
var delay_time = <?=$load_time?>;
var string = '';
var __interval;
var contentLockerLoaded = 0;
function initWidget() {

    if (delay_time > 0) {
    	setTimeout("loadWidget()", delay_time*1000);
    }else if(delay_time == -1)
    {
        //
    } else {
    	loadWidget();
    }

}


<?php  

if($include_close == 1 || $_GET['demo'] == 1)
{?>
	

function closeWidget()
{
    if(!confirm("Wait! You can access this page just by completing a short offer -- usually a quick survey or signup form. It will only take you a moment!"))
    return false;
    
	$('#widget_overlay').css('display', 'none');	
    $('#widget_wrapper').css('display', 'none');
    $('#gwframe').removeAttr('src');
}    
    
<? } ?>


function pageClose()
{
    
    <?php if(stristr($_SERVER['HTTP_USER_AGENT'], 'chrome')) { ?>

    return "Wait! You can access this page just by completing a short offer -- usually a quick survey or signup form. It will only take you a moment!";
   
    
    <? }else{ ?>
  
   
    
    if(!confirm("Wait! You can access this page just by completing a short offer -- usually a quick survey or signup form. It will only take you a moment!"))
    return false;
    <? } ?>
}



function loadWidget(){

contentLockerLoaded = 1;
    //Hiding embeds
    var embeds = document.getElementsByTagName('embed');    
	for (var emb = document.embeds, i = 0, em; em = emb[i]; i++) {
       em.setAttribute('wmode', 'transparent');
	}	    
    
   // $('#widget_wrapper').css('display', 'block');
    $('#widget_overlay').height($(document).height()+10);
	if (navigator.appName == 'Microsoft Internet Explorer') {
	 $('#widget_overlay').width($(document).width());
	}
	$('#widget_overlay').css('display', 'block');	
    $('#widget_wrapper').css('display', 'block');

    <?php
	
    if($_GET['demo'] == 1){
	?>
    
	$('#gwframe').attr('src', SITE_URL+'assets/gateway.php?gwid=<?=$gwid?>&aff_id=<?=$aff_id?>&<?=$params?>&demo=1&sess='+ssid);
    
    <?php
}else{

	?>
	$('#gwframe').attr('src', SITE_URL+'assets/gateway.php?gwid=<?=$gwid?>&aff_id=<?=$aff_id?>&<?=$params?>&sess='+ssid);

 <? }?>


	scroll(0,0);
    
}




var g = '<?=$gwid?>';
function validateGW()
{

				window.clearInterval(__interval);	
				
				__interval="";	
                	
              $.getJSON(SITE_URL+"assets/validate_status2.php?g=<?=$gwid?>&sess="+ssid+"&callback=?",
              
              function(data) {
               
               if(data.error == 1)
               { 
                   __interval = window.setInterval("validateGW()", 1200);	
               }else if(data.success == 1)
               {
                 
                    $('#widget_overlay').css('display', 'none');	
                    $('#widget_wrapper').css('display', 'none');
                    $('#gwframe').removeAttr('src');
                    window.onbeforeunload=null;
                    if(data.url != '0')
                    {
                    
                        var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
                        if(regexp.test(data.url))                        
                        document.location.href = data.url;
                        
                    }   
                    
               
               }else
               {
                    __interval = window.setInterval("validateGW()", 1200);	
               }
                
               
              });

                            
               

// __interval = window.setInterval("validateGW()", 1200);	
				
  	
}




<? if($_GET['demo'] != 1){ ?>
window.onbeforeunload = pageClose;
<? } ?>

var out = '<style type="text/css">'; 
out += '#widget_overlay { position:absolute;top:0;left:0;right:0;bottom:0; width:100%; height:100%; z-index:9999997; background-color:<?=$overlay_color?>; filter:alpha(opacity=<?=$opacity?>); opacity:<? echo $opacity/100; ?>; -moz-opacity:<? echo $opacity/100; ?>;}';
  out += '#widget_wrapper {margin-top:150px; width:100%; text-align:center; position:absolute;top:0;left:0; z-index:9999998;}';
out += '</style>';
out += '<div id="widget_overlay" style="display:none;"></div>';
out += '<div id="widget_wrapper" style="display:none;">';
<?php  

if($include_close == 1 || $_GET['demo'] == 1)
{?>
  out += '<div id="close" style="width:<?=$width?>px; margin:5px auto; text-align:right"><a href="javascript:void(0)" onclick="closeWidget()"><img src="'+SITE_URL+'assets/templates/images/cross.png" alt="Close" style="border:0" /></a></div>';
<? } ?>  
  out += '<iframe width="<?=$width?>" height="<?=$height?>" id="gwframe" src="" allowtransparency="true" frameborder="0" style="overflow-x:hidden;overflow-y:hidden; border:<?=$border_size?>px solid <?=$border_color?>; background-color:transparent; z-index:9999999;" scrollbars="no"></iframe>';
  out += '</div>';
  
if(contentLockerLoaded == 0){
document.write(out);
validateGW();
}


function Safety__PM()
{
	if(document.all){return false;}
}

function PreventRightClick(e)
{
	if(document.layers||(document.getElementById&!document.all))
		{
			if(e.which==2||e.which==3)
				{
					return false;
				}
		}
}

if(document.layers)
{
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=PreventRightClick;
}
else
{
	document.onmouseup=PreventRightClick;
	document.oncontextmenu=Safety__PM;
}

document.oncontextmenu=new Function("return false")
	

